package api

import (
	"github.com/kataras/iris/v12"
	uuid "github.com/satori/go.uuid"
	"ir/models/tables"
	"ir/services"
	"ir/utils"
	"ir/utils/response"
	"strconv"
	"time"
)

// NewRedisOrder 创建订单
func NewRedisOrder(ctx iris.Context) {
	accountId := ctx.FormValue("account-id")
	symbol := ctx.FormValue("symbol")
	side := ctx.FormValue("side")
	amount := ctx.FormValue("amount")
	price := ctx.FormValue("price")
	iSide, _ := strconv.Atoi(side)
	resp := response.New(ctx)
	if iSide != 2 {
		iSide = 1
	}
	now := time.Now().UnixNano()
	sn := uuid.NewV4().String()
	ip := utils.GetIp(ctx.Request())
	orderBook := tables.OrderBook{
		Sn:      sn,
		UserId:  accountId,
		Price:   price,
		Amount:  amount,
		Total:   amount,
		Symbol:  symbol,
		Side:    iSide,
		Created: now,
		Ip:      ip,
	}
	// 订单入库
	err := services.AddOrder(orderBook)
	if err != nil {
		resp.Error()
		return
	}
	resp.Success()
	return
}
